import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class BinaryTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public void prevOrder(TreeNode root){
        if(root == null) return;
        System.out.print(root.val + " ");
        prevOrder(root.left);
        prevOrder(root.right);
    }
    /*List<TreeNode> list = new LinkedList<>();
    public List<TreeNode> prevOrder2(TreeNode root){
        if (root == null) return null;
        list.add(root);
        prevOrder2(root.left);
        prevOrder2(root.right);
        return list;
    }*/
    public List<TreeNode> prevOrder2(TreeNode root){
        List<TreeNode> list = new ArrayList<>();
        if (root == null) return null;
        list.add(root);
        List<TreeNode> leftList = prevOrder2(root.left);
        list.addAll(leftList);
        List<TreeNode> rightList = prevOrder2(root.right);
        list.addAll(rightList);
        return list;
    }
    public void inOrder(TreeNode root){
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<Integer>();
        if(root == null) return ret;
        List<Integer> leftList = inorderTraversal(root.left);
        ret.addAll(leftList);
        ret.add(root.val);
        List<Integer> rightList = inorderTraversal(root.right);
        ret.addAll(rightList);
        return ret;
    }
    public void postOrder(TreeNode root){
        if(root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<Integer>();
        if(root == null) return ret;
        List<Integer> leftList = postorderTraversal(root.left);
        ret.addAll(leftList);
        List<Integer> rightList = postorderTraversal(root.right);
        ret.addAll(rightList);
        ret.add(root.val);
        return ret;
    }

    public int leafSize(TreeNode root){
        if(root == null) return 0;
        if(root.left == null && root.right == null){
            return 1;
        }
        return leafSize(root.left) + leafSize(root.right);
    }
    public int getHeight(TreeNode root){
        if(root == null) return 0;
        return (getHeight(root.left)>getHeight(root.right)?
                getHeight(root.left):getHeight(root.right))+1;
    }
    public int getHeight2(TreeNode root){
        if(root == null) return 0;
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return (left > right? left:right)+1;
    }


    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
}
